﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using MVCDemo.Data;
using MVCDemo.Models;
using MVCDemo.ParamModel;
using Newtonsoft.Json;

namespace MVCDemo.Controllers
{
    public class UsersController : Controller
    {
        public MVCDemoDb db = new MVCDemoDb();

        // GET: Users
        public ActionResult Index()
        {
            return View(db.Users.ToList());
        }

        // GET: Users/Details/5
        public ActionResult Details(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Users users = db.Users.Find(id);
            if (users == null)
            {
                return HttpNotFound();
            }
            return View(users);
        }

        // GET: Users/Create
        public ActionResult Create()
        {
            return View();
        }

        // POST: Users/Create
        // 为了防止“过多发布”攻击，请启用要绑定到的特定属性；有关
        // 更多详细信息，请参阅 https://go.microsoft.com/fwlink/?LinkId=317598。
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create([Bind(Include = "Id,Username,Password,CreatedAt,UpdatedAt,Version,Remarks")] Users users)
        {
            if (ModelState.IsValid)
            {
                db.Users.Add(users);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(users);
        }

        // GET: Users/Edit/5
        public ActionResult Edit(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Users users = db.Users.Find(id);
            if (users == null)
            {
                return HttpNotFound();
            }
            return View(users);
        }

        // POST: Users/Edit/5
        // 为了防止“过多发布”攻击，请启用要绑定到的特定属性；有关
        // 更多详细信息，请参阅 https://go.microsoft.com/fwlink/?LinkId=317598。
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit([Bind(Include = "Id,Username,Password,CreatedAt,UpdatedAt,Version,Remarks")] Users users)
        {
            if (ModelState.IsValid)
            {
                db.Entry(users).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(users);
        }

        // GET: Users/Delete/5
        public ActionResult Delete(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Users users = db.Users.Find(id);
            if (users == null)
            {
                return HttpNotFound();
            }
            return View(users);
        }

        // POST: Users/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            Users users = db.Users.Find(id);
            db.Users.Remove(users);
            db.SaveChanges();
            return RedirectToAction("Index");
        }


        public ActionResult Login()
        {
            return View();
        }


        public ActionResult Register()
        {
            return View();
        }

 
        [HttpPost]
        public string LoginDone(LoginModel loginModel)
        {
    
            dynamic result;
          
            var username = loginModel.Username.Trim();
        
            var password = loginModel.Password.Trim();

        
            if (username.Length > 0 && password.Length > 0)
            {
                
                var user = db.Users.Where(x => x.Username.Equals(username) && x.Password.Equals(password)).FirstOrDefault();
              
                if (user != null)
                {
                    result = new
                    {
                        code = 200,
                        msg = "欢迎你！登录成功"
                    };
                }
              
                else
                {
                    result = new
                    {
                        code = 1000,
                        msg = "用户名或密码错误，请重试！"
                    };
                }
            }
            else
            {
                result = new
                {
                    code = 1000,
                    msg = "用户名或密码不能为空，请重试！"
                };
            }
            return JsonConvert.SerializeObject(result);
        }

      
        [HttpPost]
        public string RegisterDone(RegisterModel registerModel)
        {
          
            dynamic result;

        
            var username = registerModel.Username.Trim();
       
            var password = registerModel.Password.Trim();
      
            var confirmPassword = registerModel.ConfirmPassword.Trim();

         
            if (username.Length > 0 && password.Length > 0 && confirmPassword.Length > 0 && password == confirmPassword)
            {
       
                var user = db.Users.Where(x => x.Username == username).FirstOrDefault();

             
                if (user != null)
                {
                    result = new
                    {
                        code = 1000,
                        msg = "用户已注册！，请重试"
                    };
                }
        
                else
                {
              
                    db.Users.Add(new Users
                    {
                        Username = username,
                        Password = password
                    });

                 
                    db.SaveChanges();

                    result = new
                    {
                        code = 200,
                        msg = "注册成功！"
                    };
                }
            }
          
            else
            {
                result = new
                {
                    code = 1000,
                    msg = "用户名或密码不能为空，且两次密码必须一致，请重试！"
                };
            }

            return JsonConvert.SerializeObject(result);
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }
    }
}
